package com.uuei.spring.bean;

import lombok.Data;
import lombok.ToString;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Aware接口：帮我们装配Spring底层的组件
 * 1、Bean的功能增强全部都是由 BeanPostProcessor + InitializingBean （合起来完成）
 * 2、骚操作就是BeanPostProcessor + InitializingBean ====》ApplicationContextAwareProcessor
 *
 * Person为什么能把ApplicationContext、MessageSourceAware当为自己的参数传进来
 * 		-通过实现接口的方式自动注入了ApplicationContext、MessageSource。是由BeanPostProcessor（Bean后置处理器完成的）
 */
//@Component
//@Scope(scopeName = "prototype")
public class Person implements ApplicationContextAware, MessageSourceAware {

	//@Autowired
	ApplicationContext context;// 可以得到ioc容器

	MessageSource source;

	public Person() {
		System.out.println("person创建");
	}

	public ApplicationContext getContext() {
		return context;
	}

	private String name;

	@Autowired
	private Cat cat;

	public String getName() {
		return name;
	}
	@Autowired
	public void setName(String name) {
		this.name = name;
	}

	//@Lookup //去容器中找。@Bean的这种方式注册的Person不生效 @Lookup
	public Cat getCat() {
		return cat;
	}

	@Autowired //去发现一下....
	public void setCat(Cat cat) {
		this.cat = cat;
	}


	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		//利用回调机制，把ioc容器传入
		this.context = applicationContext;
	}

	@Override
	public void setMessageSource(MessageSource messageSource) {
		this.source = messageSource;
	}
}
